﻿using System.Collections.Generic;
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer.Stream.Extension;
using System.Collections;
using System.Linq;
using System.Net.DnsToolkit.Packet.Busy;

namespace System.Net.DnsToolkit.Bastard.PacketGenerator {
    public class MultipleSameQuestions : IPacketGenerator {

        private readonly int _number;
        private readonly string _question;

        public MultipleSameQuestions()
            : this(65, "darkdns.codeplex.com") {
        }
        public MultipleSameQuestions(int number)
            : this(number, "darkdns.codeplex.com") {
        }
        public MultipleSameQuestions(int number, string question) {
            _number = number;
            _question = question;
        }

        public byte[] GetBytes() {
            return GetMessage().GetBytes();
        }

        public IMessage GetMessage() {
            var message = new Message {
                Header = new Header {
                    QueryType = QREnum.QUERY,
                    OpCode = OpCodeEnum.StandardQuery
                },
            };

            var question = new Question {
                QClass = QClassEnum.IN,
                QType = QTypeEnum.A,
                QName = new DomainName {
                    Name = _question
                }
            };
            IEnumerable<Question> questions = Enumerable.Repeat<Question>(question, _number);
            message.Questions.AddRange(questions.ToArray());
            message.Header.QuestionCount = (ushort)message.Questions.Count;

            return message;
        }
    }
}
